function rgb2dat_n(datfile, img, n);

% n - number of pixels in one line of the file
% n = 2 for 64-bit data, 4 for 128-bit data
nr = size(img,1);
nc = size(img,2);

q8 = quantizer('ufixed', [8,0]);
% q32 = quantizer('fixed', [32,20]);

            r = (img(:,:,1));
            g = (img(:,:,2));
            b = (img(:,:,3));
            
if( n == 2)
            
        %convert to column vector (row-major)
        r_1d = reshape(r', 2, nr*nc/2)';
        g_1d = reshape(g', 2, nr*nc/2)';
        b_1d = reshape(b', 2, nr*nc/2)';
        z_1d = zeros(nr*nc/2,1);
        % convert to hex
%         img_1d_hex = [num2hex(q32, img_1d(:,1)), num2hex(q32, img_1d(:,2))];
        rgb1_hex = [num2hex(q8, z_1d), num2hex(q8, r_1d(:,1)), num2hex(q8, g_1d(:,1)), num2hex(q8, b_1d(:,1))];
        rgb2_hex = [num2hex(q8, z_1d), num2hex(q8, r_1d(:,2)), num2hex(q8, g_1d(:,2)), num2hex(q8, b_1d(:,2))];
        
%         img_1d_hex = [rgb1_hex, rgb2_hex];  %[pixel 1, pixel 2]

        img_1d_hex = [rgb2_hex, rgb1_hex];  %[pixel 2, pixel 1] % Required for new DMA interface
        
elseif(n == 4)
            %convert to column vector (row-major)
        r_1d = reshape(r', 4, nr*nc/4)';
        g_1d = reshape(g', 4, nr*nc/4)';
        b_1d = reshape(b', 4, nr*nc/4)';
        z_1d = zeros(nr*nc/4,1);
        % convert to hex
%         img_1d_hex = [num2hex(q32, img_1d(:,1)), num2hex(q32, img_1d(:,2))];
        rgb1_hex = [num2hex(q8, z_1d), num2hex(q8, r_1d(:,1)), num2hex(q8, g_1d(:,1)), num2hex(q8, b_1d(:,1))];
        rgb2_hex = [num2hex(q8, z_1d), num2hex(q8, r_1d(:,2)), num2hex(q8, g_1d(:,2)), num2hex(q8, b_1d(:,2))];
        rgb3_hex = [num2hex(q8, z_1d), num2hex(q8, r_1d(:,3)), num2hex(q8, g_1d(:,3)), num2hex(q8, b_1d(:,3))];
        rgb4_hex = [num2hex(q8, z_1d), num2hex(q8, r_1d(:,4)), num2hex(q8, g_1d(:,4)), num2hex(q8, b_1d(:,4))];
        
%         img_1d_hex = [rgb1_hex, rgb2_hex];  %[pixel 1, pixel 2]

        img_1d_hex = [rgb4_hex, rgb3_hex, rgb2_hex, rgb1_hex];  %[pixel 2, pixel 1] % Required for new DMA interface
        
elseif(n == 1)
            
        %convert to column vector (row-major)
        r_1d = reshape(r', 1, nr*nc/1)';
        g_1d = reshape(g', 1, nr*nc/1)';
        b_1d = reshape(b', 1, nr*nc/1)';
        z_1d = zeros(nr*nc/2,1);
        % convert to hex
%         img_1d_hex = [num2hex(q32, img_1d(:,1)), num2hex(q32, img_1d(:,2))];
        rgb1_hex = [num2hex(q8, r_1d(:,1)), num2hex(q8, g_1d(:,1)), num2hex(q8, b_1d(:,1))];        
        
        img_1d_hex = [rgb1_hex];  %[pixel 2, pixel 1] % Required for new DMA interface
end
        % Write into the file
        fid = fopen(datfile, 'w');
        for i=1:size(img_1d_hex,1)
            if(n == 2)
                fprintf(fid,'%16s\n', img_1d_hex(i,:));
            elseif(n == 4)
                fprintf(fid,'%32s\n', img_1d_hex(i,:));
            elseif(n==1)
                fprintf(fid,'%6s\n', img_1d_hex(i,:));
            end
        end
        fclose(fid);
    